package org.example.service.impl;

import org.example.bean.Cake;
import org.example.bean.CakeCatalog;
import org.example.mapper.CakeCatalogMapper;
import org.example.service.CakeCatelogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CakeCatalogServiceImpl implements CakeCatelogService {

        private CakeCatalogMapper cakecatalogMapper;

        @Autowired
        public void setCatalogMapper(CakeCatalogMapper cakecatalogMapper) {
            this.cakecatalogMapper = cakecatalogMapper;
        }

        @Override
        public List<CakeCatalog> selectAll() {
            List<CakeCatalog> list1 = new ArrayList<>();
            List<CakeCatalog> list2 = new ArrayList<>();
            List<CakeCatalog> list3 = new ArrayList<>();
            List<CakeCatalog> list = new ArrayList<>();
            List<CakeCatalog> catalogs = cakecatalogMapper.selectAll();
            // 将所有数据按照自己的等级进行归类
            for (CakeCatalog cata : catalogs) {
                if(cata.getLevel_id() == 1) {
                    list1.add(cata);
                } else if(cata.getLevel_id() == 2) {
                    list2.add(cata);
                } else if(cata.getLevel_id() == 3) {
                    list3.add(cata);
                }
            }
            // 重新排列数据的顺序
            for(int i=0; i<list1.size(); i++) {
                CakeCatalog catalog = list1.get(i);
                list.add(catalog);
                for(int j=0; j<list2.size(); j++) {
                    CakeCatalog catalog2 = list2.get(j);
                    if(catalog2.getParent_id() == catalog.getId()) {
                        list.add(catalog2);
                        for(int k=0; k<list3.size(); k++) {
                            CakeCatalog catalog3 = list3.get(k);
                            if(catalog3.getParent_id() == catalog2.getId()) {
                                list.add(catalog3);
                            }
                        }
                    }
                }
            }
            return list;
        }

        @Override
        public int getAllCatalogCount() {
            return cakecatalogMapper.getAllCatalogCount();
        }

        @Override
        public CakeCatalog selectById(int id) {
            return cakecatalogMapper.selectById(id);
        }

        @Override
        public void insert(List<CakeCatalog> list) {
            cakecatalogMapper.insert(list);
        }

        @Override
        public void delete(int id) {
            cakecatalogMapper.delete(id);
        }

        @Override
        public CakeCatalog selectByTitle(String title) {
            return cakecatalogMapper.selectByTitle(title);
        }

        @Override
        public List<Integer> findChildren(int id) {
            return cakecatalogMapper.findChildren(id);
        }
    }

